#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <dirent.h>  
#include <unistd.h>  
#include <fcntl.h>  
#include <sys/stat.h>  
  
#define MAX_FILENAME_LENGTH 256  
  
void list_files(const char *directory) {  
    DIR *dir;  
    struct dirent *entry;  
  
    if (!(dir = opendir(directory))) {  
        perror("Error opening directory");  
        return;  
    }  
  
    while ((entry = readdir(dir)) != NULL) {  
        printf("%s\n", entry->d_name);  
    }  
  
    closedir(dir);  
}  
  
void create_file(const char *filename) {  
    FILE *file = fopen(filename, "w");  
    if (file == NULL) {  
        perror("Error creating file");  
        return;  
    }  
    fclose(file);  
    printf("File %s created successfully.\n", filename);  
}  
  
void write_to_file(const char *filename, const char *content) {  
    FILE *file = fopen(filename, "w");  
    if (file == NULL) {  
        perror("Error opening file");  
        return;  
    }  
    fputs(content, file);  
    fclose(file);  
    printf("Content written to file %s successfully.\n", filename);  
}  
  
void read_from_file(const char *filename) {  
    FILE *file = fopen(filename, "r");  
    if (file == NULL) {  
        perror("Error opening file");  
        return;  
    }  
  
    char buffer[1024];  
    while (fgets(buffer, sizeof(buffer), file)) {  
        printf("%s", buffer);  
    }  
  
    fclose(file);  
}  
  
void delete_file(const char *filename) {  
    if (remove(filename) != 0) {  
        perror("Error deleting file");  
        return;  
    }  
    printf("File %s deleted successfully.\n", filename);  
}  
  
int main() {  
    char choice, filename[MAX_FILENAME_LENGTH], content[1024];  
  
    printf("Simple File Management System\n");  
  
    while (1) {  
        printf("\n1. List files\n");  
        printf("2. Create file\n");  
        printf("3. Write to file\n");  
        printf("4. Read from file\n");  
        printf("5. Delete file\n");  
        printf("6. Exit\n");  
        printf("Enter your choice: ");  
        scanf(" %c", &choice);  
  
        switch (choice) {  
            case '1':  
                printf("Enter directory path: ");  
                scanf("%s", filename);  
                list_files(filename);  
                break;  
            case '2':  
                printf("Enter file name: ");  
                scanf("%s", filename);  
                create_file(filename);  
                break;  
            case '3':  
                printf("Enter file name: ");  
                scanf("%s", filename);  
                printf("Enter content to write: ");  
                fgets(content, sizeof(content), stdin); // Note: fgets includes newline, may need to remove it  
                write_to_file(filename, content);  
                break;  
            case '4':  
                printf("Enter file name: ");  
                scanf("%s", filename);  
                read_from_file(filename);  
                break;  
            case '5':  
                printf("Enter file name: ");  
                scanf("%s", filename);  
                delete_file(filename);  
                break;  
            case '6':  
                printf("Exiting...\n");  
                return 0;  
            default:  
                printf("Invalid choice!\n");  
        }  
    }  
  
    return 0;  
}
